<?php
include_once("DBCORE.cls.php");
class db_Dishes
{
	private $tablename = "tbldishes";
	private $db;
	public function __construct()
	{
		$this->db = new DBCORE();
		$this->db->db_connect('localhost','root','','restaurant');
		$this->db->db_query("SET NAMES 'UTF8'");	
	}
	public function select($id)
	{
		$id = !Empty($id) ? " WHERE DishesID='{$id}'": "";
		$myStrSQL = "SELECT * FROM {$this->tablename} {$id}";
		$data = $this->db->db_fetch_assoc($this->db->db_query($myStrSQL));
		return $data;
	}
	public function selectAll($id = '')
	{		
		if(empty($id))
			return false;
		$myStrSQL = "SELECT * FROM {$this->tablename} WHERE RestaurantID ='{$id}'";
		$resArr = array();
		$result = $this->db->db_query($myStrSQL);
		while($row = $this->db->db_fetch_assoc($result))
		{
			$resArr[] = $row;
		}
		return $resArr;
	}
	public function fetchDishesbyMenu($id='', $resID = '')
	{
		if(empty($id) || empty($resID))
			return false;
		$id = $this->db->db_real_escape_string($id);
		$resID = $this->db->db_real_escape_string($resID);
		$myStrSQL = "SELECT * FROM {$this->tablename} WHERE MenuID ='{$id}' AND RestaurantID='{$resID}'";
		$resArr = array();
		$result = $this->db->db_query($myStrSQL);
		while($row = $this->db->db_fetch_assoc($result))
		{
			$resArr[] = $row;
		}
		return $resArr;
	}
	public function insertDishes(array $data) 
	{
		$DishesID = $data['DishesID'];
		$RestaurantID = $data['$RestaurantID'];
		$FullName = $data['FullName'];
		$Price = $data['Price'];
		$Description = $data['Description'];
		$CateDishesID = $data['CateDishesID'];
		$myStrSQL = "INSERT INTO {$this->tablename} VALUES('$DishesID','$RestaurantID','$FullName','$Price','$Description','$CateDishesID')";
		$this->db->db_query($myStrSQL);
		$result = $this->db->db_affected_rows();
		return $result;
	}
	public function delete($id)
	{
		$id = !empty($id) ? " WHERE DishesID={$id}": "";
		$myStrSQL="DELETE FROM {$this->tablename} {$id}";
		$result = $this->db->db_affected_rows();
		return $result;
	}
	public function deleteAll()
	{
		$myStrSQL="DELETE FROM {$this->tablename} WHERE 1";
		$result = $this->db->db_affected_rows();
		return $result;
	}
	/*public function updates(array $data, $condistion)	
	{
		if(empty($condistion))
			return;

	}*/
	public function findbyCondition(array $data)	
	{
		$myStrSQL = "SELECT * FROM {$this->tablename} WHERE 1 ";
		if(!empty($data['DishesID']))
			$myStrSQL = $myStrSQL." AND DishesID = '".$data['DishesID']."'";
		if(!empty($data['RestaurantID']))
			$myStrSQL = $myStrSQL." AND RestaurantID = '".$data['RestaurantID']."'";
		if(!empty($data['FullName']))
			$myStrSQL = $myStrSQL." AND FullName = '".$data['FullName']."'";
		if (!empty($data['Price'])) 
			$myStrSQL = $myStrSQL." AND Price = '".$data['Price']."'";
		if (!empty($data['Description'])) 
			$myStrSQL = $myStrSQL." AND Description = '".$data['Description']."'";
		if (!empty($data['CateDishesID'])) 
			$myStrSQL = $myStrSQL." AND CateDishesID = '".$data['CateDishesID']."'";
	}
}


/*$test = new db_Dishes();
$abc= $test->fetchDishesbyMenu('MN01','RE0001');
var_dump($abc);*/
?>




